package Campus.help.user.controller;

import Campus.help.common.exception.payException;
import Campus.help.common.utils.R;
import Campus.help.user.service.UserCountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@RestController
@RequestMapping("/user/count")
@Api(tags = "用户账号信息")
public class UserCountController {

    @Autowired
    private UserCountService userCountService;

    @GetMapping("/getM")
    @ApiOperation("获取账户金额")
    public R getMoney(){
        return userCountService.getMoney();
    }
    @PostMapping("/addM")
    @ApiOperation("充值")
    public R addMoney(@RequestParam("amount")BigDecimal amount){
        return userCountService.addMoney(amount);
    }

    @PostMapping("/payfor")
    @ApiOperation("支付")
    public R payForMoney(@RequestParam("amount")BigDecimal amount){
        try {
            return userCountService.payForMoney(amount);
        } catch (payException e) {
            return R.error(400,"支付失败！");
        }
    }
    @PostMapping("/return")
    public R returnAmount(@RequestParam("amount")BigDecimal amount,@RequestParam("userId")Long userId){
        try {
            return userCountService.returnMoney(amount,userId);
        } catch (payException e) {
            return R.error(400,"退款失败！");
        }
    }
    @PostMapping("/complete")
    public R complete(@RequestParam("amount")BigDecimal amount,@RequestParam("userId")Long userId){
            return userCountService.complete(userId,amount);
    }

}